﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class OrderDetailController
    {
        public IList<OrderDetail> GetOrderDetailByOrder(Order order)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<OrderDetail>().ToList()
                    .Where(o => o.Order.Id == order.Id)
                    .Select(o => o)
                    .ToList();
            }
        }

        public IList<OrderDetailData> GetAllOrderDetailDataByOrder(Order order)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var orderDetails = session.Query<OrderDetail>().ToList()
                    .Where(o => o.Order.Id == order.Id)
                    .Select(o => o)
                    .ToList();

                return orderDetails.Select(orderDetail => new OrderDetailData(orderDetail.Id,
                    orderDetail.Order.Id.ToString(), orderDetail.Comic.Title, 
                    orderDetail.OrderDetailStatus.Status)).ToList();
            }
        }

        public IList<Comic> GetAllNotAvailableComic()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var orderDetails = session.Query<OrderDetail>().ToList();
                return orderDetails
                    .Where(o => o.OrderDetailStatus.Status == "Belum Kembali")
                    .Select(o => o.Comic).ToList();
            }
        }

        public int CheckOrder(Order order)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var orderDetails = session.Query<OrderDetail>().ToList()
                    .Where(o => o.Order.Id == order.Id)
                    .Select(o => o)
                    .ToList();

                var total = orderDetails.Count;
                var returned = orderDetails.Count(orderDetail => orderDetail.OrderDetailStatus.Status == "Sudah Kembali");

                if (returned == total)
                {
                    return 1;
                }
                
                if (returned == 0)
                {
                    return -1;
                }

                if (returned < total)
                {
                    return 0;
                }
            }

            return 0;
        }

        public void AddOrderDetail(OrderDetail orderDetail)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(orderDetail);
                    transaction.Commit();
                }
            }
        }

        public void EditOrderDetail(OrderDetail newOrderDetail)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(newOrderDetail);
                    transaction.Commit();
                }
            }
        }

        public void DeleteOrderDetail(OrderDetail orderDetail)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(orderDetail);
                    transaction.Commit();
                }
            }
        }
    }

    public class OrderDetailData
    {
        public OrderDetailData(Guid id, string order, string comic, string status)
        {
            Id = id;
            Order = order;
            Comic = comic;
            OrderDetailStatus = status;
        }

        public Guid Id { get; set; }
        public string Order { get; set; }
        public string Comic { get; set; }
        public string OrderDetailStatus { get; set; }
    }
}
